﻿using System.Web.UI;

namespace AutoCompleteTextBox
{
    public class AutoCompleteTextBoxDataSourceManager
    {
        private AutocompleteDataSource AutoCompleteDataSource { get; set; }

        private Control Control { get; set; }

        private string DataSourceId { get; set; }

        private string YUIPath { get; set; }

        public string SourceID { get; set; }

        public AutoCompleteTextBoxDataSourceManager(AutocompleteDataSource AutoCompleteDataSource, Control Control, string DataSourceId, string YUIPath)
        {
            this.AutoCompleteDataSource = AutoCompleteDataSource;
            this.Control = Control;
            this.DataSourceId = DataSourceId;
            this.YUIPath = YUIPath;
            Control.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), YUIPath, YUIPath);
            RenderDataSource();
        }
        public void ConnectToDataSource()
        {
            Control c = ControlHelper.FindControl(Control, DataSourceId);
            AutoCompleteDataSource = c as AutocompleteDataSource;
        }

        public void RenderDataSource()
        {
            if (AutoCompleteDataSource == null)
            {
                ConnectToDataSource();
            }

            if (AutoCompleteDataSource != null)
            {
                SourceID = AutoCompleteDataSource.RenderDataSource();
            }
        }
    }
}
